import { Toast } from 'vant';
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      activeNum:0,
      isShowTabbar:true,
    },
    children:[
      {
        path: '/home/searchPopup',
        name: 'SearchPopup',
        component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/SearchPopup.vue')
      }
    ]
  },
  {
    path: '/topic',
    name: 'Topic',
    meta:{
      activeNum:1,
      isShowTabbar:true,
    },
    component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/Topic.vue')

  },
  {
    path: '/category',
    name: 'Category',
    meta:{
      activeNum:2,
      isShowTabbar:true,
    },
    component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/Category.vue')

  },
  {
    path: '/cart',
    name: 'Cart',
    meta:{
      activeNum:3,
      isShowTabbar:true,
    },
    component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/Cart.vue')

  },
  {
    path: '/user',
    name: 'User',
    meta:{
      activeNum:4,
      isShowTabbar:true,
    },
    component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/User.vue')

  },
  {
    path: '/producDetail',
    name: 'ProducDetail',
    component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/ProducDetail.vue')

  },
  
  
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局前置路由守卫
 router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
      //定义本地登录数据 
  let token = localStorage.getItem('token'); 
       if(to.path=="/cart"){
          //点击进入判断是否登录
          if(token){
               next()
          }else{
            //错误提示
          Vue.prototype.$toast('请先登录')
          //1s跳转到登录页
          setTimeout(() => {
             next('/user')
          }, 1000);
          }
      }
      //顺利通过路由去路
      next()
})

export default router
